/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.Exercise.NewExercise;

import com.myapp.model.ejercicios.Ejercicios;
import com.myapp.model.estados.Estados;
import com.myapp.model.temas.Temas;
import com.myapp.model.usuarios.Usuarios;
import com.myapp.svc.Exercise.ExerciseSvc;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.MessageResources;

/**
 *
 * @author Tarde
 */
public class NewExerciseAction extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String FAIL = "fail";
    private static final String USR = "usuario";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        ActionForward toReturn;

        HttpSession sesion = request.getSession();
        Usuarios usr = (Usuarios) sesion.getAttribute(USR);

        ExerciseSvc exerSvc = new ExerciseSvc();
        Ejercicios exer = new Ejercicios();
        Ejercicios exerBack = new Ejercicios();

        Integer idejercicio = ((NewExerciseForm) form).getIdejercicio();
        String titulo = (((NewExerciseForm) form).getTitulo());
        String descripcion = (((NewExerciseForm) form).getDescripcion());
        Integer idtema = (((NewExerciseForm) form).getTema());

        Temas tem = new Temas();
        tem.setIdtema(idtema);
        if (idejercicio != 0) {
            exer.setIdejercicio(idejercicio);
        }
        exer.setTitulo(titulo);
        exer.setDescripcion(descripcion);
        exer.setTemas(tem);

        exer.setUsuarios(usr);
        exer.setMaestro((byte) 0);
        exer.setPuntuacion((byte) 0);

        Date fecha = new Date();

        exer.setFecha(fecha);

        exer.setSecreto((byte) 0);

        Estados estado = new Estados();
        estado.setIdestado((byte) 6);

        exer.setEstados(estado);

        exerBack = exerSvc.newOrUpdateExercise(exer);

        if (exerBack != null) {
            toReturn = mapping.findForward(SUCCESS);

        } else {

            ActionErrors errors = new ActionErrors();

            //Recupero el fichero de recursos
            MessageResources m;
            m = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);

            //Idioma actual
            java.util.Locale idioma = (java.util.Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);

            errors.add("result", new ActionMessage("error.erroroperation", m.getMessage(idioma, "error.erroroperation")));

            saveErrors(request, errors);

            toReturn = mapping.findForward(FAIL);

        }

        return toReturn;
    }
}
